package com.c6906.vdam.dps.config.redis;

import com.alibaba.fastjson.JSON;
import com.c6906.vdam.dps.common.DateUtils;
import com.c6906.vdam.dps.entity.TerminalBusinessInfo;
import com.c6906.vdam.dps.entity.TerminalData;
import com.c6906.vdam.dps.entity.VehiclesLast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class RedisUtils {
    private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);

    private static final String LAST_HISTORY_DATA_PRE = "LastHistoryData";
    private static final String PRE_REDIS_KEY = "SN";
    private static final String TERMINAL_HEARTBEAT_KEY = "LastLoc:";
    private static final String PRE_GPS_DATA_REDIS_KEY = "GPS_DATA";

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //-------------------------- 基础操作 --------------------------
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    //-------------------------- 高级操作 --------------------------
    public TerminalBusinessInfo terminalInfoIsExistFormRedis(String sn) {
        Object info = redisTemplate.opsForValue().get(sn);
        if (info instanceof String) {
            try {
                return JSON.parseObject((String) info, TerminalBusinessInfo.class);
            } catch (Exception e) {
                logger.error("terminalInfoIsExistFormRedis JSON解析异常", e);
            }
        }
        return new TerminalBusinessInfo();
    }

    public void saveLastDatasForHistory(int num, List<TerminalData> terminalDataList) {
        for (TerminalData terminalData : terminalDataList) {
            String lastHistory = PRE_REDIS_KEY + terminalData.getTerminalSn() + LAST_HISTORY_DATA_PRE;
            redisTemplate.opsForList().leftPush(lastHistory, JSON.toJSONString(terminalData));
            redisTemplate.opsForList().trim(lastHistory, 0, num - 1);
        }
    }


    public List<String> getLastDatasForHistory(String terminalSn) {
        String lastHistory = PRE_REDIS_KEY + terminalSn + LAST_HISTORY_DATA_PRE;
        return stringRedisTemplate.opsForList().range(lastHistory, 0, -1);
    }

    public VehiclesLast getOneTerminalStatuses(String terminalSn) {
        String gpsKey = PRE_GPS_DATA_REDIS_KEY + terminalSn;
        VehiclesLast vehiclesLast = new VehiclesLast();

        Object gpsData = redisTemplate.opsForValue().get(gpsKey);
        if (gpsData instanceof String) {
            try {
                vehiclesLast = JSON.parseObject((String) gpsData, VehiclesLast.class);
                String heartbeatKey = TERMINAL_HEARTBEAT_KEY + terminalSn;
                String lastHeartbeat = (String) stringRedisTemplate.opsForHash().get(heartbeatKey, "time");
                if (lastHeartbeat != null) {
                    vehiclesLast.setStrLastHeartbeatTime(
                            String.valueOf(DateUtils.strToDate(lastHeartbeat).getTime())
                    );
                }
            } catch (Exception e) {
                logger.error("getOneTerminalStatuses解析异常", e);
            }
        }
        return vehiclesLast;
    }
}